package com.battery.service.impl;

import com.battery.dao.UserDao;
import com.battery.dao.Vip_OrderDao;
import com.battery.pojo.Vip_Order;
import com.battery.service.VipOrderService;
import com.battery.util.result.Result;
import com.battery.util.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VipOrderServiceImpl implements VipOrderService {
    @Autowired
    private Vip_OrderDao vipOrderDao;
    @Autowired
    private UserDao userDao;


    @Override
    public Result getVipOrderZt(int user_id) {
        return Result.ok().message("订单已完成");
    }

    @Override
    public Result searchVipOrder() {
        try {
            List<Vip_Order> allVipOrders = vipOrderDao.findAllVipOrders();
            return Result.ok(ResultCodeEnum.SUCCESS).data("allVipOrders",allVipOrders);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找出错");
        }
    }

    @Override
    public Result searchVipOrderById(int vip_order_id) {
        try {
            Vip_Order vipOrder = vipOrderDao.findOne(vip_order_id);
            if (vipOrder!=null){
                return Result.ok().data("vipOrder",vipOrder);
            }else {
                return Result.error().message("未查询到该vip订单信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找出错");
        }
    }

    @Override
    public Result update(Vip_Order VipOrder) {
        try{
            if (vipOrderDao.updateMer(VipOrder)>0){
                return Result.ok().message("修改成功");
            }
            else return Result.error().message("修改失败：未找到对应vip订单详细");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应vip订单信息");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result insert(Vip_Order vipOrder) {
        try{
            if (vipOrderDao.insert(vipOrder)>0){
                return Result.ok().message("添加成功");
            }
            else return Result.error().message("添加失败：vip订单信息错误");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("添加失败：已经存在对应vip订单");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("添加失败："+e.toString());
        }
    }

    @Override
    public Result delete(int vip_order_id) {
        try{
            if (vipOrderDao.delete(vip_order_id)>0){
                return Result.ok().message("删除成功");
            }
            else return Result.error().message("删除失败：该vip订单不能删除");
        } catch (Exception e){
            System.err.println(e);
            return Result.error().message("删除失败："+e.toString());
        }
    }
}
